<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Banner extends Backend_Controller {

	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
		// 加载album模型
		$this->load->model('backend/Album_model', 'Album');
		// 加载Album模型
		$this->load->model('backend/Album_model', 'Album');
		// 加载验证类
		$this->load->library('form_validation');
	}

	/**
	 * 显示后台个人中心下的查看分类
	 */
	public function index() {
		$this->isValidRequest();
		$result = $this->Album->getBannerAll($this->page, $this->input->post());
		$this->load->view('backend/banner/index', $result);
	}

	/**
	 * 添加广告图
	 */
	public function add() {		
		$this->isValidRequest();
		
		if ($this->isPost()) {
			$this->form_validation->validateData(array(
				'banner_id',
				'link',
				'title'
					), array(
				array(
					'banner_id',
					'required|integer|greater_than[0]'
				),
				array(
					'title',
					'required'
				)
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'source_id' => $this->form_validation->banner_id,
				'link' => $this->form_validation->link,
				'title' => $this->form_validation->title,
				'created_at' => time()
			);
			$result = $this->Album->update($data, array('id'=>$data['source_id']));
			if ($result) {
				$this->renderJson(200, '', array(
					'navTabId' => 'backendbannerindex',
					'callbackType' => 'closeCurrent'
				));
			}
			$this->renderJson(300);
		}
		$this->load->view('backend/banner/add');
	}

	/**
	 * 删除广告图
	 */
	public function delete() {
		$ids = $this->input->post_get('ids');
		$imgs = $this->Album->getAll($ids, 'img');
		foreach ($imgs as $v) {
			if(file_exists($v['img']) && is_writable($v['img'])) {
				unlink($v);
			}
		}
		if ($this->Album->deleteAll($ids)) {
			$this->renderJson(200);
		}
		$this->renderJson(300);
	}

	/**
	 * 修改广告图
	 */
	public function edit() {
		$this->isValidRequest();
		if ($this->isPost()) {
			$this->form_validation->validateData(array(
				'banner_id',
				'link',
				'title'
					), array(
				array(
					'banner_id',
					'required|integer|greater_than[0]'
				),
				array(
					'title',
					'required'
				)
			));

			if ($this->form_validation->hasErrors()) {
				$this->renderJson(300, $this->form_validation->error);
			}

			$data = array(
				'link' => $this->form_validation->link,
				'title' => $this->form_validation->title
			);

			$this->Album->update($data, array('id'=>$this->form_validation->banner_id));
			
			$this->renderJson(200, '', array(
				'navTabId' => 'backendbannerindex',
				'callbackType' => 'closeCurrent'
			));

			$this->renderJson(300);
		}
		$id = intval($this->get('id'));
		$data = $this->Album->getAll($id);
		$this->load->view('backend/banner/edit', $data[0]);
	}

	public function upload() {
		if (!empty($_FILES)) {
			$id = $this->post('id', 0);
			
			$config ['upload_path'] = config_item('upload_path') . '/banner/' . date('Ymd');
			if (!is_dir($config ['upload_path'])) {
				mkdir($config ['upload_path'], 0755, true);
			}
			$config ['file_name'] = time() . rand(1000, 9999);
			$config ['allowed_types'] = 'jpg|bmp|jpeg|gif|png';

			$data = array(
				'status' => 300,
				'msg' => ''
			);

			$this->load->library('upload', $config);
			if (!$this->upload->do_upload('filedata')) {
				$data ['msg'] = $this->upload->display_errors();
			} else {
				$dataInsert = array(
					'img' => $config ['upload_path'] . '/' . $this->upload->data('file_name'),
					'type' => '2', // 类型默认链接
					'from' => '3', // 来源广告图
				);
				if(!empty($id)) {
					$row = $this->Album->get_row(array('id'=>$id));
					if(!empty($row['img']) && file_exists($row['img'])) {
						unlink($row['img']);
					}
					$this->Album->update($dataInsert, array('id'=>$id));
					$data['id'] = $id;
				} else  {
					$dataInsert['created_at'] = time();
					$data ['id'] = $this->Album->addOne($dataInsert);
				}
				$data ['filename'] = $dataInsert['img'];
				$data ['status'] = 200;
				$data ['msg'] = base_url($dataInsert ['img']);
			}
			if ($data ['status'] == 200) {
				$this->renderJson(200, $data ['msg'], array(
					'id' => $data ['id'],
					'filename' => $data['filename'],
					'id]' => $data ['id'],
					'filename]' => $data['filename']
				));
			} else {
				show_error($data ['msg']);
			}
		}
		$this->load->view('backend/banner/upload');
	}

}
